import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Reporter extends Thread
{
  private DataGatherer gatherer;

  private ConcurrentLinkedQueue<ReportData> reportDataQueue;

  public Reporter(DataGatherer gatherer,
      ConcurrentLinkedQueue<ReportData> queue)
  {
    this.gatherer = gatherer;
    this.reportDataQueue = queue;
  }

  /*
   * public void report() { Iterator i =
   * gatherer.getKeywords().entrySet().iterator(); ArrayList<Map.Entry<String,
   * Integer>> data = new ArrayList<Map.Entry<String, Integer>>();
   * while(i.hasNext()) { data.add((Map.Entry<String, Integer>)i.next()); }
   * NumberFormat f = new DecimalFormat("#.####"); double l =
   * gatherer.getParseTimePerPage();
   * 
   * System.out.println("*************************************************");
   * System.out.println("Pages Retrieved: " + gatherer.getPagesRetrieved());
   * System.out.println("Average words per page: " +
   * gatherer.getAvgWordsPerPage()); System.out.println("Average URLs per
   * page: \n" + gatherer.getAvgUrlsPerPage()); System.out .println("Key
   * Words:"); for(Map.Entry<String, Integer> m : data) { String word =
   * m.getKey(); Integer totalHits = m.getValue(); double average =
   * (totalHits > 0? totalHits/(double) gatherer.getPagesRetrieved(): 0);
   * System.out.println(word + "\n Average: " + average + "\n Total Hits:" +
   * totalHits + "\n"); } System.out.println("Page Limit: " +
   * DataGatherer.PAGE_LIMIT); System.out.println("Average Parse Time per
   * Page: " + l/1000 + " sec"); d//ouble totalTime =
   * gatherer.getTotalRunningTime(System.currentTimeMillis());
   * //System.out.println("Total Run Time " + f.format(totalTime/1000) + "
   * sec");
   * System.out.println("*************************************************\n"); }
   */

  public void report(ReportData rd)
  {
    ArrayList<Map.Entry<String, Integer>> data = rd.getKeywordData();
    NumberFormat f = new DecimalFormat("#.###");
    double l = rd.getAvgParseTime();

    System.out.println("*************************************************");
    System.out.println("Pages Retrieved: " + rd.getPagesRetrieved());
    System.out.println("Average words per page: "
        + f.format(rd.getAvgWordsPerPage()));
    System.out.println("Average URLs per page: "
        + f.format(rd.getAvgUrlsPerPage()) + "\n");
    System.out.println("Key Words:");
    for (Map.Entry<String, Integer> m : data)
    {
      String word = m.getKey();
      Integer totalHits = m.getValue();
      double average = (totalHits > 0 ? totalHits
          / (double) rd.getPagesRetrieved() : 0);
      System.out.println(word + "   Average Hits: " + f.format(average)
          + "   Total Hits: " + totalHits + "\n");
    }
    System.out.println("Page Limit: " + DataGatherer.PAGE_LIMIT);
    System.out.println("Average Parse Time per Page: " + l / 1000 + " sec");
    double totalTime = System.currentTimeMillis() - rd.getStartTime();
    System.out.println("Total Run Time " + f.format(totalTime / 1000)
        + " sec");
    System.out
        .println("*************************************************\n");
  }

  public void run()
  {
    int pageReported = 0;
    while (pageReported < DataGatherer.PAGE_LIMIT)
    {
      if (!(reportDataQueue.isEmpty()))
      {
        ReportData data = reportDataQueue.poll();

        pageReported = data.getPagesRetrieved();
        report(data);
      }
    }
  }

}
